<!--评论模块-->
<template>
  <div class="container">
    <el-form ref="elForm" :model="comment" :rules="rules" size="medium" label-width="100px">
      <el-form-item prop="content" style="margin-left:-26px">
        <div class="author-comment">
          <div class="input-new-content">
            <img :src="$store.state.user.avatar" alt />
            <el-input
              v-model="comment.content"
              type="textarea"
              placeholder="请输入评论"
              :maxlength="255"
              :autosize="{minRows: 3, maxRows: 4}"
              :style="{width: '84%'}"
            ></el-input>
          </div>
        </div>
      </el-form-item>
      <el-form-item size="large">
        <el-button
          type="default"
          :style="{width:'6.5%',marginLeft:'840px',position:'relative',bottom:'10px',backgroundColor:'#67C23A',color:'white'}"
          round
          @click="submitForm"
        >提交</el-button>
      </el-form-item>
    </el-form>
    <div class="comment" v-for="item in comments" :key="item.id">
      <div class="info">
        <img class="avatar" :src="item.avatar" width="36" height="36" />
        <div class="right">
          <div class="name">{{item.nickname}}</div>
          <div class="date">{{item.modifiedTime}}</div>
        </div>
      </div>
      <div class="content">{{item.content}}</div>
      <div class="control">
        <!-- 点赞 -->
        <span class="like" :class="{active: item.isLike}" @click="likeClick(item)">
          <i class="iconfont icon-like"></i>
          <span class="like-num">{{item.likeNum > 0 ? item.likeNum + '人赞' : '赞'}}</span>
        </span>
        <span class="comment-reply" v-show="!item.children" @click="showCommentInput(item)">
          <i class="iconfont icon-comment"></i>
          <span>回复</span>
        </span>
      </div>
      <div class="reply">
        <div class="item" v-for="reply in item.children" :key="reply.id">
          <div class="reply-content">
            <img class="avatar" :src="reply.avatar" width="36" height="36" />
            <span class="from-name">{{reply.nickname}}</span>
            <span>:</span>
            <span class="to-name" v-if="reply.parentName != null">@{{reply.parentName}}</span>
            <span>{{reply.content}}</span>
          </div>
          <div class="reply-bottom">
            <span>{{reply.modifiedTime}}</span>
            <span class="reply-text" @click="showChildCommentInput(item, reply)">
              <i class="iconfont icon-comment"></i>
              <span>回复</span>
            </span>
          </div>
        </div>
        <div
          class="write-reply"
          v-if="item.children&&item.children.length > 0"
          @click="showCommentInput(item)"
        >
          <i class="el-icon-edit"></i>
          <span class="add-comment">添加新评论</span>
        </div>
        <transition name="fade">
          <div class="input-wrapper" v-if="showItemId == item.id">
            <el-input
              class="gray-bg-input"
              v-model="comment.content"
              type="textarea"
              :placeholder="placeHolderContent"
              :rows="3"
              autofocus
            ></el-input>
            <div class="btn-control">
              <span class="cancel" @click="cancel">取消</span>
              <el-button class="btn" type="success" round @click="commitComment">确定</el-button>
            </div>
          </div>
        </transition>
      </div>
    </div>
  </div>
</template>
<script>
import { insertComment } from "@/api/comment";
export default {
  props: {
    comments: {
      type: Array,
      required: true
    },
    catalogueId: {
      type: String,
      required: true
    }
  },
  components: {},
  data() {
    return {
      showItemId: "",
      showReplyId: "",
      placeHolderContent: "请输入您的评论",
      comment: {
        content: null,
        parentId: null,
        catalogueId: this.catalogueId,
        avatar: this.$store.state.user.avatar,
        nickname: this.$store.state.user.nickName,
        userId: this.$store.state.user.id
      },
      rules: {
        content: [
          {
            required: true,
            message: "评论内容不能为空",
            trigger: "submit"
          }
        ]
      }
    };
  },
  computed: {},
  methods: {
    /**
     * 点赞
     */
    likeClick(item) {
      if (item.isLike === null) {
        item.likeNum++;
      } else {
        if (item.isLike) {
          item.likeNum--;
        } else {
          item.likeNum++;
        }
        item.isLike = !item.isLike;
      }
    },

    /**
     * 点击取消按钮
     */
    cancel() {
      this.comment.parentId = null;
      this.showItemId = "";
      this.showReplyId = "";
      this.comment.content = "";
    },

    /**
     * 提交评论
     */
    async commitComment() {
      if (this.showReplyId !== "") {
        this.comment.parentId = this.showReplyId;
      } else if (this.showItemId !== "") {
        this.comment.parentId = this.showItemId;
      }
      this.post();
    },
    async post() {
      const { data: res } = await insertComment(this.comment);
      this.cancel();
      this.$emit("reload");
    },
    submitForm() {
      console.log(this.comment);
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        else {
          this.post();
        }
        // TODO 提交表单
      });
    },
    /**
     * 点击评论按钮显示输入框
     * item: 当前大评论
     * reply: 当前回复的评论
     */
    showChildCommentInput(item, reply) {
      if (reply) {
        this.placeHolderContent = "@" + reply.nickname + " ";
      } else {
        this.comment.content = "";
      }
      if (item.id != undefined) {
        this.showItemId = item.id;
      }
      if (reply.id != undefined) {
        this.showReplyId = reply.id;
      }
    },
    showCommentInput(item, reply) {
      if (reply) {
        this.placeHolderContent = "@" + reply.nickname + " ";
      } else {
        this.comment.content = "";
        this.showItemId = "";
        this.showReplyId = "";
      }
      this.showItemId = item.id;
    }
  }
};
</script>

<style scoped lang="scss">
@import "../../public/scss/comment";

.container {
  padding: 0 10px;
  box-sizing: border-box;
  margin: 40px;
  .comment {
    margin: 0 90px;
    margin-bottom: 20px;
    display: flex;
    flex-direction: column;
    padding: 10px;
    border-bottom: 1px solid $border-fourth;
    .info {
      display: flex;
      align-items: center;
      .avatar {
        border-radius: 50%;
      }
      .right {
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        .name {
          text-align: left;
          font-size: 16px;
          color: $text-main;
          margin-bottom: 5px;
          font-weight: 500;
        }
        .date {
          font-size: 12px;
          color: $text-minor;
        }
      }
    }
    .content {
      font-size: 16px;
      color: $text-main;
      text-align: left;
      box-sizing: border-box;
      line-height: 20px;
      padding: 10px;
      padding-left: 45px;
    }
    .control {
      margin-left: 40px;
      display: flex;
      align-items: center;
      font-size: 14px;
      color: $text-minor;
      .like {
        display: flex;
        align-items: center;
        margin-right: 20px;
        cursor: pointer;
        &.active,
        &:hover {
          color: $color-main;
        }
        .iconfont {
          font-size: 14px;
          margin-right: 5px;
        }
      }
      .comment-reply {
        display: flex;
        align-items: center;
        cursor: pointer;
        &:hover {
          color: $text-333;
        }
        .iconfont {
          font-size: 16px;
          margin-right: 5px;
        }
      }
    }
    .reply {
      margin-left: 32px !important;
      margin: 10px 0;
      border-left: 2px solid $border-first;
      .item {
        margin: 0 10px;
        padding: 10px 0;
        border-bottom: 1px dashed $border-third;
        .reply-content {
          display: flex;
          align-items: center;
          font-size: 14px;
          color: $text-main;
          .from-name {
            color: $color-main;
          }
          .to-name {
            color: $color-main;
            margin-left: 5px;
            margin-right: 5px;
          }
        }
        .reply-bottom {
          display: flex;
          align-items: center;
          margin-top: 6px;
          font-size: 12px;
          color: $text-minor;
          .reply-text {
            display: flex;
            align-items: center;
            margin-left: 10px;
            cursor: pointer;
            &:hover {
              color: $text-333;
            }
            .icon-comment {
              margin-right: 5px;
            }
          }
        }
      }
      .write-reply {
        display: flex;
        align-items: center;
        font-size: 14px;
        color: $text-minor;
        padding: 10px;
        cursor: pointer;
        &:hover {
          color: $text-main;
        }
        .el-icon-edit {
          margin-right: 5px;
        }
      }
      .fade-enter-active,
      fade-leave-active {
        transition: opacity 0.5s;
      }
      .fade-enter,
      .fade-leave-to {
        opacity: 0;
      }
      .input-wrapper {
        padding: 10px;
        .gray-bg-input,
        .el-input__inner {
          background-color: #67c23a;
        }
        .btn-control {
          display: flex;
          justify-content: flex-end;
          align-items: center;
          padding-top: 10px;
          .cancel {
            font-size: 16px;
            color: $text-normal;
            margin-right: 20px;
            cursor: pointer;
            &:hover {
              color: $text-333;
            }
          }
          .confirm {
            font-size: 16px;
          }
        }
      }
    }
  }
  .input-new-content {
    text-align: left;
    width: 100%;
    margin-left: 27px;
    & > img {
      position: relative;
      bottom: 20px;
      margin-right: 10px;
      width: 36px;
      height: 36px;
      border-radius: 50%;
    }
  }
}
</style>